home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / patch12.zip / PATCH.C < prev    next >
C/C++ Source or Header  |  1990-05-30  |  20KB  |  825 lines

  1. char rcsid[] =
  2.     "$Header: patch.c,v 2.0.1.6 88/06/22 20:46:39 lwall Locked $";
  3.  
  4. /* patch - a program to apply diffs to original files
  5.  *
  6.  * Copyright 1986, Larry Wall
  7.  *
  8.  * This program may be copied as long as you don't try to make any
  9.  * money off of it, or pretend that you wrote it.
  10.  *
  11.  * $Log:    patch.c,v $
  12.  * Revision 2.0.1.6  88/06/22  20:46:39  lwall
  13.  * patch12: rindex() wasn't declared
  14.  * 
  15.  * Revision 2.0.1.5  88/06/03  15:09:37  lwall
  16.  * patch10: exit code improved.
  17.  * patch10: better support for non-flexfilenames.
  18.  * 
  19.  * Revision 2.0.1.4  87/02/16  14:00:04  lwall
  20.  * Short replacement caused spurious "Out of sync" message.
  21.  * 
  22.  * Revision 2.0.1.3  87/01/30  22:45:50  lwall
  23.  * Improved diagnostic on sync error.
  24.  * Moved do_ed_script() to pch.c.
  25.  * 
  26.  * Revision 2.0.1.2  86/11/21  09:39:15  lwall
  27.  * Fuzz factor caused offset of installed lines.
  28.  * 
  29.  * Revision 2.0.1.1  86/10/29  13:10:22  lwall
  30.  * Backwards search could terminate prematurely.
  31.  * 
  32.  * Revision 2.0  86/09/17  15:37:32  lwall
  33.  * Baseline for netwide release.
  34.  * 
  35.  * Revision 1.5  86/08/01  20:53:24  lwall
  36.  * Changed some %d's to %ld's.
  37.  * Linted.
  38.  * 
  39.  * Revision 1.4  86/08/01  19:17:29  lwall
  40.  * Fixes for machines that can't vararg.
  41.  * Added fuzz factor.
  42.  * Generalized -p.
  43.  * General cleanup.
  44.  * 
  45.  * 85/08/15 van%ucbmonet@berkeley
  46.  * Changes for 4.3bsd diff -c.
  47.  *
  48.  * Revision 1.3  85/03/26  15:07:43  lwall
  49.  * Frozen.
  50.  * 
  51.  * Revision 1.2.1.9  85/03/12  17:03:35  lwall
  52.  * Changed pfp->_file to fileno(pfp).
  53.  * 
  54.  * Revision 1.2.1.8  85/03/12  16:30:43  lwall
  55.  * Check i_ptr and i_womp to make sure they aren't null before freeing.
  56.  * Also allow ed output to be suppressed.
  57.  * 
  58.  * Revision 1.2.1.7  85/03/12  15:56:13  lwall
  59.  * Added -p option from jromine@uci-750a.
  60.  * 
  61.  * Revision 1.2.1.6  85/03/12  12:12:51  lwall
  62.  * Now checks for normalness of file to patch.
  63.  * 
  64.  * Revision 1.2.1.5  85/03/12  11:52:12  lwall
  65.  * Added -D (#ifdef) option from joe@fluke.
  66.  * 
  67.  * Revision 1.2.1.4  84/12/06  11:14:15  lwall
  68.  * Made smarter about SCCS subdirectories.
  69.  * 
  70.  * Revision 1.2.1.3  84/12/05  11:18:43  lwall
  71.  * Added -l switch to do loose string comparison.
  72.  * 
  73.  * Revision 1.2.1.2  84/12/04  09:47:13  lwall
  74.  * Failed hunk count not reset on multiple patch file.
  75.  * 
  76.  * Revision 1.2.1.1  84/12/04  09:42:37  lwall
  77.  * Branch for sdcrdcf changes.
  78.  * 
  79.  * Revision 1.2  84/11/29  13:29:51  lwall
  80.  * Linted.  Identifiers uniqified.  Fixed i_ptr malloc() bug.  Fixed
  81.  * multiple calls to mktemp().  Will now work on machines that can only
  82.  * read 32767 chars.  Added -R option for diffs with new and old swapped.
  83.  * Various cosmetic changes.
  84.  * 
  85.  * Revision 1.1  84/11/09  17:03:58  lwall
  86.  * Initial revision
  87.  * 
  88.  */
  89.  
  90. #include "INTERN.h"
  91. #include "common.h"
  92. #include "EXTERN.h"
  93. #include "version.h"
  94. #include "util.h"
  95. #include "pch.h"
  96. #include "inp.h"
  97.  
  98. /* procedures */
  99.  
  100. void reinitialize_almost_everything();
  101. void get_some_switches();
  102. LINENUM locate_hunk();
  103. void abort_hunk();
  104. void apply_hunk();
  105. void init_output();
  106. void init_reject();
  107. void copy_till();
  108. void spew_output();
  109. void dump_line();
  110. bool patch_match();
  111. bool similar();
  112. void re_input();
  113. void my_exit();
  114.  
  115. /* Apply a set of diffs as appropriate. */
  116.  
  117. main(argc,argv)
  118. int argc;
  119. char **argv;
  120. {
  121.     LINENUM where;
  122.     LINENUM newwhere;
  123.     LINENUM fuzz;
  124.     LINENUM mymaxfuzz;
  125.     int hunk = 0;
  126.     int failed = 0;
  127.     int failtotal = 0;
  128.     int i;
  129.  
  130.     setbuf(stderr, serrbuf);
  131.     for (i = 0; i<MAXFILEC; i++)
  132.     filearg[i] = Nullch;
  133.     Mktemp(TMPOUTNAME);
  134.     Mktemp(TMPINNAME);
  135.     Mktemp(TMPREJNAME);
  136.     Mktemp(TMPPATNAME);
  137.  
  138.     /* parse switches */
  139.     Argc = argc;
  140.     Argv = argv;
  141.     get_some_switches();
  142.     
  143.     /* make sure we clean up /tmp in case of disaster */
  144.     set_signals(0);
  145.  
  146.     for (
  147.     open_patch_file(filearg[1]);
  148.     there_is_another_patch();
  149.     reinitialize_almost_everything()
  150.     ) {                    /* for each patch in patch file */
  151.  
  152.     if (outname == Nullch)
  153.         outname = savestr(filearg[0]);
  154.     
  155.     /* initialize the patched file */
  156.     if (!skip_rest_of_patch)
  157.         init_output(TMPOUTNAME);
  158.     
  159.     /* for ed script just up and do it and exit */
  160.     if (diff_type == ED_DIFF) {
  161.         do_ed_script();
  162.         continue;
  163.     }
  164.     
  165.     /* initialize reject file */
  166.     init_reject(TMPREJNAME);
  167.     
  168.     /* find out where all the lines are */
  169.     if (!skip_rest_of_patch)
  170.         scan_input(filearg[0]);
  171.     
  172.     /* from here on, open no standard i/o files, because malloc */
  173.     /* might misfire and we can't catch it easily */
  174.     
  175.     /* apply each hunk of patch */
  176.     hunk = 0;
  177.     failed = 0;
  178.     out_of_mem = FALSE;
  179.     while (another_hunk()) {
  180.         hunk++;
  181.         fuzz = Nulline;
  182.         mymaxfuzz = pch_context();
  183.         if (maxfuzz < mymaxfuzz)
  184.         mymaxfuzz = maxfuzz;
  185.         if (!skip_rest_of_patch) {
  186.         do {
  187.             where = locate_hunk(fuzz);
  188.             if (hunk == 1 && where == Nulline && !force) {
  189.                         /* dwim for reversed patch? */
  190.             if (!pch_swap()) {
  191.                 if (fuzz == Nulline)
  192.                 say1(
  193. "Not enough memory to try swapped hunk!  Assuming unswapped.\n");
  194.                 continue;
  195.             }
  196.             reverse = !reverse;
  197.             where = locate_hunk(fuzz);  /* try again */
  198.             if (where == Nulline) {        /* didn't find it swapped */
  199.                 if (!pch_swap())         /* put it back to normal */
  200.                 fatal1("Lost hunk on alloc error!\n");
  201.                 reverse = !reverse;
  202.             }
  203.             else if (noreverse) {
  204.                 if (!pch_swap())         /* put it back to normal */
  205.                 fatal1("Lost hunk on alloc error!\n");
  206.                 reverse = !reverse;
  207.                 say1(
  208. "Ignoring previously applied (or reversed) patch.\n");
  209.                 skip_rest_of_patch = TRUE;
  210.             }
  211.             else {
  212.                 ask3(
  213. "%seversed (or previously applied) patch detected!  %s -R? [y] ",
  214.                 reverse ? "R" : "Unr",
  215.                 reverse ? "Assume" : "Ignore");
  216.                 if (*buf == 'n') {
  217.                 ask1("Apply anyway? [n] ");
  218.                 if (*buf != 'y')
  219.                     skip_rest_of_patch = TRUE;
  220.                 where = Nulline;
  221.                 reverse = !reverse;
  222.                 if (!pch_swap())  /* put it back to normal */
  223.                     fatal1("Lost hunk on alloc error!\n");
  224.                 }
  225.             }
  226.             }
  227.         } while (!skip_rest_of_patch && where == Nulline &&
  228.             ++fuzz <= mymaxfuzz);
  229.  
  230.         if (skip_rest_of_patch) {        /* just got decided */
  231.             Fclose(ofp);
  232.             ofp = Nullfp;
  233.         }
  234.         }
  235.  
  236.         newwhere = pch_newfirst() + last_offset;
  237.         if (skip_rest_of_patch) {
  238.         abort_hunk();
  239.         failed++;
  240.         if (verbose)
  241.             say3("Hunk #%d ignored at %ld.\n", hunk, newwhere);
  242.         }
  243.         else if (where == Nulline) {
  244.         abort_hunk();
  245.         failed++;
  246.         if (verbose)
  247.             say3("Hunk #%d failed at %ld.\n", hunk, newwhere);
  248.         }
  249.         else {
  250.         apply_hunk(where);
  251.         if (verbose) {
  252.             say3("Hunk #%d succeeded at %ld", hunk, newwhere);
  253.             if (fuzz)
  254.             say2(" with fuzz %ld", fuzz);
  255.             if (last_offset)
  256.             say3(" (offset %ld line%s)",
  257.                 last_offset, last_offset==1L?"":"s");
  258.             say1(".\n");
  259.         }
  260.         }
  261.     }
  262.  
  263.     if (out_of_mem && using_plan_a) {
  264.         Argc = Argc_last;
  265.         Argv = Argv_last;
  266.         say1("\n\nRan out of memory using Plan A--trying again...\n\n");
  267.         continue;
  268.     }
  269.     
  270.     assert(hunk);
  271.     
  272.     /* finish spewing out the new file */
  273.     if (!skip_rest_of_patch)
  274.         spew_output();
  275.     
  276.     /* and put the output where desired */
  277.     ignore_signals();
  278.     if (!skip_rest_of_patch) {
  279.         if (move_file(TMPOUTNAME, outname) < 0) {
  280.         toutkeep = TRUE;
  281.         chmod(TMPOUTNAME, filemode);
  282.         }
  283.         else
  284.         chmod(outname, filemode);
  285.     }
  286.     Fclose(rejfp);
  287.     rejfp = Nullfp;
  288.     if (failed) {
  289.         failtotal += failed;
  290.         if (!*rejname) {
  291.         Strcpy(rejname, outname);
  292. #ifndef FLEXFILENAMES
  293.         {
  294.             char *rindex();
  295.             char *s = rindex(rejname,'/');
  296.  
  297.             if (!s)
  298.             s = rejname;
  299.             if (strlen(s) > 13)
  300.             if (s[12] == '.')    /* try to preserve difference */
  301.                 s[12] = s[13];    /* between .h, .c, .y, etc. */
  302.             s[13] = '\0';
  303.         }
  304. #endif
  305.         Strcat(rejname, REJEXT);
  306.         }
  307.         if (skip_rest_of_patch) {
  308.         say4("%d out of %d hunks ignored--saving rejects to %s\n",
  309.             failed, hunk, rejname);
  310.         }
  311.         else {
  312.         say4("%d out of %d hunks failed--saving rejects to %s\n",
  313.